# -*- coding: utf-8 -*-
#
# Author:: Jonny
# Date:: 2022/4/26


import os
import time

from compat.project_paths import project_tmp_images_path


def keep_tmp_images_clean(num=100):
    file_name_list = os.listdir(project_tmp_images_path)

    file_info = []
    for file_name in file_name_list:
        file_path_abs = os.path.join(project_tmp_images_path, file_name)
        t = os.path.getmtime(file_path_abs)
        timeStruce = time.localtime(t)
        times = time.strftime("%Y%m%d%H%M%S", timeStruce)
        file_info.append([file_path_abs, times])

    file_info_sorted = sorted(file_info, key=lambda x: x[1], reverse=True)

    # 删除除近x以外的
    for item in file_info_sorted[num:]:
        try:
            file_path_abs = item[0]
            os.remove(file_path_abs)
        except:
            pass
